
import * as path from 'path';
import * as fs from 'fs';


export const isExistFile = function (filePath: string) {
  try {
    //fs.accessSync(filePath, fs.constants.F_OK | fs.constants.W_OK);
    return fs.statSync(filePath).isFile();
  } catch (e) {
    return false;
  }
}

export const getFilePath = function (filePath: string) {
  if (isExistFile(filePath)) {
    return filePath;
  }
  const extensions = [
    ".tsx",
    ".ts",
    ".jsx",
    ".js",
  ];
  for (let i = 0; i < extensions.length; i++) {
    const _filepth = filePath + extensions[i];
    if (isExistFile(_filepth)) {
      return _filepth;
    }
  }
  for (let i = 0; i < extensions.length; i++) {
    const _filepth = path.join(filePath, 'index' + extensions[i]);
    if (isExistFile(_filepth)) {
      return _filepth;
    }
  }
  return '';
}